/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VisualizarEscolas.java
 *
 * Created on 24/06/2011, 15:28:37
 */
package GUI.escola;

import GUI.IntegranteEscola.IntegranteEscola;
import bd.MySql;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import util.MySqlTableModel;

/**
 *
 * @author Gabriel
 */
public class VisualizarEscolas extends javax.swing.JFrame {

    private String idEscola;

    /** Creates new form VisualizarEscolas */
    public VisualizarEscolas() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jPopupMenu2 = new javax.swing.JPopupMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        painelFiltros = new javax.swing.JPanel();
        labelNome = new javax.swing.JLabel();
        textNome = new javax.swing.JTextField();
        labelEndereco = new javax.swing.JLabel();
        textEndereco = new javax.swing.JTextField();
        labelCor1 = new javax.swing.JLabel();
        labelCor2 = new javax.swing.JLabel();
        textCor1 = new javax.swing.JTextField();
        textCor2 = new javax.swing.JTextField();
        btAplicar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        scrollResultados = new javax.swing.JScrollPane();
        tableResultados = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jMenuItem1.setText("Editar");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem1);

        jMenuItem2.setText("Excluir");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jPopupMenu1.add(jMenuItem2);

        jMenuItem3.setText("Editar");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jPopupMenu2.add(jMenuItem3);

        jMenuItem4.setText("Excluir");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jPopupMenu2.add(jMenuItem4);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Escolas");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        painelFiltros.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros"));

        labelNome.setText("Nome:");

        labelEndereco.setText("Endereço:");

        labelCor1.setText("Cor 1:");

        labelCor2.setText("Cor 2:");

        btAplicar.setText("Aplicar");
        btAplicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAplicarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout painelFiltrosLayout = new javax.swing.GroupLayout(painelFiltros);
        painelFiltros.setLayout(painelFiltrosLayout);
        painelFiltrosLayout.setHorizontalGroup(
            painelFiltrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelFiltrosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelFiltrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelEndereco)
                    .addComponent(labelNome)
                    .addComponent(labelCor1)
                    .addComponent(labelCor2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelFiltrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textNome, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                    .addComponent(textEndereco, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
                    .addGroup(painelFiltrosLayout.createSequentialGroup()
                        .addGroup(painelFiltrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(textCor1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textCor2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 57, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 192, Short.MAX_VALUE)
                        .addComponent(btAplicar)))
                .addContainerGap())
        );
        painelFiltrosLayout.setVerticalGroup(
            painelFiltrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelFiltrosLayout.createSequentialGroup()
                .addGroup(painelFiltrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNome)
                    .addComponent(textNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelFiltrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEndereco)
                    .addComponent(textEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelFiltrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCor1)
                    .addComponent(textCor1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                .addGroup(painelFiltrosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCor2)
                    .addComponent(textCor2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btAplicar))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Escolas"));

        tableResultados.setModel(new TableModelEscolas("SELECT * FROM escola"));
        tableResultados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableResultadosMouseClicked(evt);
            }
        });
        scrollResultados.setViewportView(tableResultados);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollResultados, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrollResultados, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Integrantes"));

        jTable1.setModel(new DefaultTableModel());
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(painelFiltros, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painelFiltros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // exibe os dados da tabela escola de acordo com os filtros utilizados pelo usuario
    private void btAplicarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAplicarActionPerformed
        // TODO add your handling code here:
        int and_count = 0;
        String query = "SELECT * FROM escola ";

        // aplica o filtro de nome
        if (!this.textNome.getText().equals("")) {
            if (and_count == 0) {
                query = query + "WHERE nomeCompleto LIKE \"%" + this.textNome.getText() + "%\" ";
                and_count++;
            } else {
                query = query + "AND nomeCompleto LIKE \"%" + this.textNome.getText() + "%\" ";
            }
        }

        // aplica o filtro de endereço
        if (!this.textEndereco.getText().equals("")) {
            if (and_count == 0) {
                query = query + "WHERE enderecoEscola LIKE \"%" + this.textEndereco.getText() + "%\" ";
                and_count++;
            } else {
                query = query + "AND enderecoEscola LIKE \"%" + this.textEndereco.getText() + "%\" ";
            }
        }

        // aplica o filtro de primeira cor
        if (!this.textCor1.getText().equals("")) {
            if (and_count == 0) {
                query = query + "WHERE cor1 LIKE \"%" + this.textCor1.getText() + "%\" ";
                and_count++;
            } else {
                query = query + "AND cor1 LIKE \"%" + this.textCor1.getText() + "%\" ";
            }
        }

        // aplica o filtro de segunda cor
        if (!this.textCor2.getText().equals("")) {
            if (and_count == 0) {
                query = query + "WHERE cor2 LIKE \"%" + this.textCor2.getText() + "%\" ";
                and_count++;
            } else {
                query = query + "AND cor2 LIKE \"%" + this.textCor2.getText() + "%\" ";
            }
        }

        System.out.println(query);
        this.tableResultados.setModel(new TableModelEscolas(query));
    }//GEN-LAST:event_btAplicarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_formWindowClosing

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_formWindowClosed

    private void tableResultadosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableResultadosMouseClicked
        // TODO add your handling code here:
        if(evt.getButton() == MouseEvent.BUTTON3) {
            tableResultados.removeRowSelectionInterval(0, tableResultados.getRowCount()-1);
            tableResultados.addRowSelectionInterval(tableResultados.rowAtPoint(evt.getPoint()), tableResultados.rowAtPoint(evt.getPoint()));
            this.jPopupMenu1.show(tableResultados, evt.getX(), evt.getY());
        } else if(evt.getButton() == MouseEvent.BUTTON1) {
            Object ObjectIdEscola = tableResultados.getValueAt(tableResultados.getSelectedRow(), 0);
            if (ObjectIdEscola != null) {
                String id = ObjectIdEscola.toString();

                this.idEscola = id;
                String colunas[] = {"CPF","Nome"};
                String query = "SELECT Integrante.cpf,pessoa.nome FROM (SELECT * FROM integrante_escola WHERE idEscola=" + id + ") as Integrante JOIN pessoa ON Integrante.cpf=pessoa.cpf";
                System.out.println(query);
                jTable1.setModel(new MySqlTableModel(query, colunas));
            }
        }
    }//GEN-LAST:event_tableResultadosMouseClicked

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        String id = tableResultados.getValueAt(tableResultados.getSelectedRow(), 0).toString();

        String query = "DELETE FROM escola WHERE idEscola=" + id;

        System.out.println(query);

        MySql conn = new MySql(GUI.Main.config);

        conn.insert(query);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        // TODO add your handling code here:
        String id = tableResultados.getValueAt(tableResultados.getSelectedRow(), 0).toString();

        new CadastroEscola(id).setVisible(true);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        String id = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();

        new IntegranteEscola(this.idEscola, id).setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        if(evt.getButton() == MouseEvent.BUTTON3) {
            jTable1.removeRowSelectionInterval(0, jTable1.getRowCount()-1);
            jTable1.addRowSelectionInterval(jTable1.rowAtPoint(evt.getPoint()), jTable1.rowAtPoint(evt.getPoint()));
            this.jPopupMenu2.show(jTable1, evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
        String id = jTable1.getValueAt(jTable1.getSelectedRow(), 0).toString();

        String query = "DELETE FROM integrante_escola WHERE idEscola=" + this.idEscola + " AND cpf=" + id;

        System.out.println(query);

        MySql conn = new MySql(GUI.Main.config);

        conn.insert(query);
    }//GEN-LAST:event_jMenuItem4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VisualizarEscolas().setVisible(true);
            }
        });
    }

    // exibe os dados da tabela escola em uma tabela na tela
    private class TableModelEscolas extends DefaultTableModel {

        public TableModelEscolas(String query) {
            MySql conn = new MySql(GUI.Main.config);

            /* Construindo colunas */
            this.addColumn("ID");
            this.addColumn("Nome");
            this.addColumn("Endereço");
            this.addColumn("Cor 1");
            this.addColumn("Cor 2");

            ArrayList<ArrayList<String>> resultado = conn.getTable(query);
            for (int i = 0; i < resultado.size(); i++) {
                String rowData[] = new String[resultado.get(i).size()];
                for (int j = 0; j < resultado.get(0).size(); j++) {
                    rowData[j] = resultado.get(i).get(j);
                }
                this.addRow(rowData);
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAplicar;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JPopupMenu jPopupMenu2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel labelCor1;
    private javax.swing.JLabel labelCor2;
    private javax.swing.JLabel labelEndereco;
    private javax.swing.JLabel labelNome;
    private javax.swing.JPanel painelFiltros;
    private javax.swing.JScrollPane scrollResultados;
    private javax.swing.JTable tableResultados;
    private javax.swing.JTextField textCor1;
    private javax.swing.JTextField textCor2;
    private javax.swing.JTextField textEndereco;
    private javax.swing.JTextField textNome;
    // End of variables declaration//GEN-END:variables
}
